package com.anjubao.parking.municipal.roadside.service;

import com.anjubao.parking.municipal.common.utils.U;
import com.anjubao.parking.municipal.parkinglot.dto.EntryShotUrlDTO;
import com.anjubao.parking.municipal.roadside.feign.ParkingRecordFeignClient;
import com.anjubao.parking.municipal.roadside.form.CapturePicForm;
import com.anjubao.parking.municipal.roadside.mapper.ParkingImageMapper;
import com.anjubao.parking.municipal.roadside.mapper.ParkingRecordMapper;
import com.anjubao.parking.municipal.roadside.model.ParkingRecord;
import com.anjubao.parking.municipal.roadside.model.ParkingImage;
import com.anjubao.parking.municipal.roadside.utils.DateUtils;
import com.anjubao.parking.municipal.roadside.utils.ParkingUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class ParkingImageService {
    @Autowired
    private ParkingImageMapper parkingImageMapper;
    @Autowired
    private ParkingUtils parkingUtils;
    @Autowired
    private ParkingRecordFeignClient parkingRecordFeignClient;
    @Value("${upload_image_url}")
    private String uploadImageUrl;
    @Autowired
    private ParkingRecordMapper parkingRecordMapper;


    /**
     * 保存进场后定时数据
     * @param picForm
     */
    public void save(CapturePicForm picForm){
        ParkingRecord parkingRecord=parkingRecordMapper.selectById(picForm.getParkingRecordId());
        if (parkingRecord==null) return;
        ParkingImage parkingImage=new ParkingImage();
        parkingImage.setId(U.id());
        parkingImage.setParkingRecordId(picForm.getParkingRecordId());
        parkingImage.setCaptureImage(parkingUtils.uploadImage(uploadImageUrl,picForm.getImageData()));
        parkingImage.setCaptureTime(DateUtils.string2Date(picForm.getCaptureTime()));
        parkingImage.setImageType(picForm.getType());
        parkingImageMapper.insert(parkingImage);
        if (picForm.getType()==2){
            EntryShotUrlDTO entryShotUrlDTO = new EntryShotUrlDTO();
            entryShotUrlDTO.setLocalRecordId(picForm.getParkingRecordId());
            entryShotUrlDTO.setParkingLotCode(parkingRecord.getParkingLotCode());
            entryShotUrlDTO.setSnapshots(parkingImageMapper.getInAndOutPic(picForm.getParkingRecordId()));
            parkingRecordFeignClient.snapShotUrl(entryShotUrlDTO);
        }
    }
}
